//
//  HJForgotPasswordController.m
//  KaiChat
//
//  Created by lx on 2023/10/18.
//

#import "HJForgotPasswordController.h"
#import "HJTitleTextField.h"
#import "HJSendCodeController.h"
#import "HJMinePresent.h"

@interface HJForgotPasswordController ()

@property (nonatomic, weak) HJStyle1Button *nextButton;
@property (nonatomic, weak) HJTitleTextField *emailTextField;

@end

@implementation HJForgotPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
        
    UILabel *titleLabel = [UILabel lableWithSuperView:self.view font:UIFontSemiboldMake(20) color:UIColor.tj_333333Color title:@"Retrieve Account" textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight + 20);
        make.left.offset(leftRightMargin);
    }];
    
    UILabel *subTitleLabel = [UILabel lableWithSuperView:self.view font:UIFontMake(14) color:UIColor.tj_868391Color title:@"Please enter your email address" textAlignment:NSTextAlignmentLeft];
    [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom);
        make.left.equalTo(titleLabel);
    }];
    
    HJTitleTextField *emailTextField = [HJTitleTextField addToView:self.view];
    emailTextField.titleLabel.text = @"";
    emailTextField.textField.placeholder = @"Only supports G-mail";
    emailTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    emailTextField.textField.keyboardType = UIKeyboardTypeASCIICapable;
    [emailTextField.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [emailTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(subTitleLabel.mas_bottom).offset(28);
        make.height.offset(82);
    }];
    
    HJStyle1Button *nextButton = [HJStyle1Button buttonWithSuperView:self.view font:UIFontMediumMake(14) color:nil title:@"Next"];
    [nextButton addTarget:self action:@selector(didClickNextButton) forControlEvents:UIControlEventTouchUpInside];
    nextButton.enabled = NO;
    [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(20);
        make.height.offset(buttonHeight);
        make.bottom.inset(10 + TabbarSafeBottomMargin);
    }];
    
    _nextButton = nextButton;
    _emailTextField = emailTextField;
}


- (void)textFieldDidChange:(UITextField *)textField {
    _nextButton.enabled = !isEmptyString(textField.text);
//    _nextButton.enabled = [textField.text.lowercaseString containsString:@"@gmail.com"];
}

- (void)didClickNextButton {
    NSString *email = _emailTextField.textField.text;
    
    if (![email.lowercaseString containsString:@"@gmail.com"]) {
        [HJHUD showMessage:@"Email format error"];
        return;
    }
    
    [HJHUD showLoadingInView:self.view];
    [HJMinePresent sendResetPasswordEmailCode:email complete:^(NSError * _Nullable error) {
        if (!error) {
            HJSendCodeController *sendCodeController = [[HJSendCodeController alloc] init];
            sendCodeController.email = email;
            sendCodeController.sendCodeType = HJSendCodeTypeForgetPassword;
            [self.navigationController pushViewController:sendCodeController];
        }
    }];
    

}

- (BOOL)hiddenBackgroundView {
    return NO;
}
 

@end
